//
//  WXPAuthPersonalContentHeaderView.m
//  MexicoLoan
//
//  Created by WXP on 2025/3/2.
//
//

#import "WXPAuthPersonalContentHeaderView.h"

@interface WXPAuthPersonalContentHeaderView ()<UITextFieldDelegate>

@end

@implementation WXPAuthPersonalContentHeaderView

#pragma mark - lazy
- (UIView *)viewContent{
    if (!_viewContent) {
        _viewContent = [UIView new];
        _viewContent.backgroundColor = COLOR_WHITE;
        _viewContent.width = SCREEN_WIDTH-LEFT_MARDIN*2;
    }
    return _viewContent;
}

// 教育水平
- (WXPAuthSelectView *)educationView
{
    if (_educationView == nil) {
        _educationView = [WXPAuthSelectView  new];
        ModelBtn * model = [ModelBtn new];
        model.title = @"Nivel de Educación";
        [_educationView resetViewWithModel:model];
        WEAKSELF
        _educationView.blockSelect = ^{
            [weakSelf endEditing:true];
            [GlobalMethod buryPointWithPageName:KEY_VerifyPerson featureName:KEY_Education_Level action:KEY_GET_FOCUS type:KEY_select];
            [GlobalMethod hitPointWithPageName:KEY_VerifyPerson action:KEY_Education_Level_Focus extInfo:nil];
            NSMutableArray * array = [NSMutableArray new];
            for (int i = 0; i < weakSelf.educationAry.count; i ++) {
                NSDictionary * dic = weakSelf.educationAry[i];
                ModelBtn * model = [ModelBtn new];
                model.title = [dic stringValueForKey:KEY_remark];
                model.content = [dic stringValueForKey:KEY_typeName];
                [array addObject:model];
            }
            [weakSelf.pickerView resetViewWithAry:array withTag:1];
            UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
            [window addSubview:weakSelf.pickerView];
        };
    }
    return  _educationView;
}

// 邮件
- (WXPAuthSelectView *)emailView
{
    if (_emailView == nil) {
        _emailView = [WXPAuthSelectView  new];
        ModelBtn * model = [ModelBtn new];
        model.title = @"Correo Electrónico";
        model.content = @"Por ejemplo xxxxxx@gmail.com";
        [_emailView resetViewWithModel:model];
        _emailView.imageArrow.hidden = true;
        _emailView.tfContent.userInteractionEnabled = true;
        _emailView.tfContent.delegate = self;
    }
    return  _emailView;
}
- (WXPAuthPickerView *)pickerView
{
    if (_pickerView == nil) {
        _pickerView = [WXPAuthPickerView  new];
        WEAKSELF
        _pickerView.blockDidSelectRow = ^(ModelBtn *model, NSInteger tag) {
            if(tag == 1){
                [GlobalMethod buryPointWithPageName:KEY_VerifyPerson featureName:KEY_Education_Level action:KEY_LOST_FOCUS type:KEY_select];
                if(model != nil){
                    weakSelf.educationView.tfContent.text = model.title;
                    weakSelf.educationQualification = model.content;
                }
            }
            if(weakSelf.blockResetAuthBtn){
                weakSelf.blockResetAuthBtn();
            }
        };
    }
    return  _pickerView;
}

#pragma mark - UITextFieldDelegate
// 获得焦点
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [GlobalMethod buryPointWithPageName:KEY_VerifyPerson featureName:KEY_EMail_Input action:KEY_GET_FOCUS type:KEY_input];
    [GlobalMethod hitPointWithPageName:KEY_VerifyPerson action:KEY_EMail_Focus extInfo:nil];

}
// 失去焦点
- (void)textFieldDidEndEditing:(UITextField *)textField {
    [GlobalMethod buryPointWithPageName:KEY_VerifyPerson featureName:KEY_EMail_Input action:KEY_LOST_FOCUS type:KEY_input];
}
// 粘贴行为
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if(textField == self.emailView.tfContent){
        if (isStr(string)) {
            [GlobalMethod buryPointWithPageName:KEY_VerifyPerson featureName:KEY_EMail_Input action:KEY_INPUT type:KEY_input];
        }
    }
    // 检测是否为粘贴行为
    NSString * trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([UIPasteboard generalPasteboard].string.length > 0 && [trimmedString isEqualToString:[UIPasteboard generalPasteboard].string]) {
        if(textField == self.emailView.tfContent){
            [GlobalMethod buryPointWithPageName:KEY_VerifyPerson featureName:KEY_EMail_Input action:KEY_PARSE type:KEY_input];
        }
        return true;
    }
    return YES;
}


#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.width = SCREEN_WIDTH;
        [self configUI];
    }
    return self;
}

- (void)configUI{
    self.viewContent = [UIView initWithViews:@[
        self.educationView,
        self.emailView,
//        [GlobalMethod getViewWithHeight:Ratio(16)],
    ] width:SCREEN_WIDTH-2*LEFT_MARDIN];
    self.viewContent.backgroundColor = COLOR_WHITE;
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.viewContent.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(16, 16)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = path.CGPath;
    self.viewContent.layer.mask = maskLayer;
    self.viewContent.centerXTop = XY(self.width/2, 0);
    [self addSubview:self.viewContent];
    self.height = self.viewContent.bottom;
}

@end
